   Ext.namespace('VAS');
 VAS.RoleForm=Ext.extend(Ext.FormPanel,{
 	
 	  initComponent:function(){
 	  	var _this=this;
 	  	_this.params;
 	  	_this.url='admin/system/RoleAction!';
 	  	_this.treeUrl='admin/system/RoleAction!getRights.action';
 	  	if(typeof(_this.record)==='object'&&_this.actionType.toLowerCase().indexOf('edit')>-1){
 	  		_this.url=_this.url+'update.action';
 	  			_this.params={roleId:_this.record.get('id')}
 	  
 	  	}else{
 	  		_this.url=_this.url+'add.action';
 	  	}
 	  	
 	  	
		var tree=	new VAS.CheckTreePanel({
			id		: 'hahah',
			width  : 300,
			height : 350,
            url    : _this.treeUrl,
            params : _this.params
		});
 	  	
      var config = {
        url:_this.url,
        frame:true,
        bodyStyle:'padding:5px 15px 0 ',
        items: [
        	{
                xtype: 'hidden',
                name: 'id',
                allowBlank:false
            },{
            	xtype: 'textfield',
                fieldLabel: '角色名称',
                labelStyle      : "width:120px;text-align:left;color:red",
                name: 'roleName',
                width  : 300,
                allowBlank:false
                ,blankText : '角色名称不允许为空'
                ,maxLength:50
                ,maxLengthText : "长度不能超过50个字符"
                ,regex: /\S/
				,regexText  : '角色名称不允许为空格'
                ,size:24
            },{
            	xtype: 'textfield',
                fieldLabel: '描述',
                labelStyle      : "width:120px;text-align:left",
                name: 'description',
                width  : 300                
                ,maxLength:100
                ,maxLengthText : "长度不能超过100个字符"
                ,regex: /\S/
				,regexText  : '描述不允许为空格'
            },{
					layout: "table",
					columnWidth : 1,
					items: [
						{
							xtype           : "panel",
							layout          : "form",
							width           : 122,
							items           : {
								xtype       : "label", fieldLabel: "分配权限",
								labelStyle  : "text-align:left;color:red"
							}
						},
						tree
					
					]
			}
            
        ]
	 };

	 Ext.apply(this, config);
	 Ext.apply(this.initialConfig, config);
	 VAS.RoleForm.superclass.initComponent.apply(this, arguments); 
	}
	
	//获取父节点，递归实现无限级父节点
	,getParentNode : function(node,selectObj){
		var parentNode=node.parentNode;
		if(parentNode.id!='root'){
			if(!parentNode.attributes.checked){
					selectObj.selectedNodeId += ","+parentNode.id;
			}
			this.getParentNode(parentNode,selectObj);
		}
	}
	
	,getCheckedNode:function(){
		var _this = this;
		var selNodes = this.items.itemAt(3).items.itemAt(1).getChecked();
		
		var selectObj = {};
		selectObj.selectedNodeId = '';
		Ext.each(selNodes, function(node){
			if (node.id != "root") {
				if (selectObj.selectedNodeId == "") {
					selectObj.selectedNodeId += node.id;
				} else {
					selectObj.selectedNodeId += ","+node.id;
				}
				_this.getParentNode(node,selectObj);
			}
		});
		return selectObj.selectedNodeId;
	}

});
 
 
VAS.RoleWin = function($config) {
	var _this = this;	
	_this.roleForm=new  VAS.RoleForm({
		record       : $config.record,
		actionType   : $config.actionType
	});	
	
	_this.getPostdata=function(){		    
     	var rightIds=_this.roleForm.getCheckedNode();
    	var postdata=_this.roleForm.getForm().getValues();
    	postdata.rightIds=rightIds;    
    	if(postdata.rightIds=='')
    	{
    		postdata.failure=true;
    		postdata.msg='请选择权限';
    	}
    	return postdata;	
	}
	
	_this.saveAndCloseCallback=function(){
	   $config.grid.reload();
	   _this.close();
	}
 	_this.saveCallback=function(){
 		$config.grid.reload();
		_this.roleForm.getForm().reset();
		Ext.getCmp('hahah').getRootNode().reload();
	}
      
	var thisConfig = {
		autoRoll:true,
		items:_this.roleForm,
		height:$config.height,
		width:$config.width
	};
	
	var save = new Ext.ux.Button({
    	text:'保存',
    	actionType:'save'
    	,owner:_this
    	,postdata:_this.getPostdata,
    	action:_this.roleForm.url
    	,callback:_this.saveCallback
    	,form:_this.roleForm
	});
	var saveAndClose = new Ext.ux.Button({
    	text:'保存并关闭',
    	actionType:'save'
    	,owner:_this
    	,postdata:_this.getPostdata,
    	action:_this.roleForm.url
    	,callback:_this.saveAndCloseCallback
    	,form:_this.roleForm
		
	});
	var close = new Ext.ux.Button({
        xtype:'comButton',
    	text:'关闭',
    	actionType:'close'
    	,owner:_this,
    	action:''
       ,callback:_this.callback
       ,postdata:{}
		
	});
	
	if(typeof $config.record == 'object'  && $config.actionType.toLowerCase().indexOf('edit')>-1){
		Ext.apply(thisConfig, {buttons : [saveAndClose,close]});
	}
	else{
		Ext.apply(thisConfig, {buttons : [save,saveAndClose,close]});
	}
	
	var _config = $config || {};
	Ext.apply(_this, Ext.apply(thisConfig, _config));
	VAS.RoleWin.superclass.constructor.call(_this);
};

Ext.extend(VAS.RoleWin, Ext.Window, {	
	manager:wgroup,
	loadRecord:function(){	
		this.items.itemAt(0).getForm().loadRecord(this.record);
	}
});

Ext.reg('role-win', VAS.RoleWin);
   
  